package com.xueqiu.movieplaymanagersystem.controller.admin;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.xueqiu.movieplaymanagersystem.model.JsonResult;
import com.xueqiu.movieplaymanagersystem.utils.UploadUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @author xueqiu
 */
@Controller
public class UploadController {
    @Value("${requestVisitPath}")
    private String requestVisitPath;

    /**
     * 上传电影封面
     *
     * @param file    电影封面图；
     * @param request 请求
     * @return 失败的消息或者成功的电影封面图URL
     */
    @RequestMapping(value = "/admin/upload", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public JsonResult<String> images(MultipartFile file, HttpServletRequest request) {
        JsonResult<String> jsonResult = new JsonResult<>();
        //生成电影封面图文件；获取该文件URL
        String imageUrl = UploadUtils.upload(file, requestVisitPath, request);
        if (imageUrl == null) {
            jsonResult.Error("上传电影封面图片失败！");
        } else {
            ArrayList<String> resultList = new ArrayList<>();
            resultList.add(imageUrl);
            jsonResult.Success(imageUrl,resultList);
        }
        return jsonResult;
    }

    /**
     * 上传电影媒体文件
     * @param file 电影媒体文件；格式不限
     * @param request 请求
     * @return 失败的消息或者成功的电影媒体文件URL
     */
    @RequestMapping(value = "/admin/uploadMovie", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public JsonResult<String> movie(MultipartFile file, HttpServletRequest request) {
        JsonResult<String> jsonResult = new JsonResult<>();
        //生成电影媒体文件；获取该文件URL
        String movieUrl = UploadUtils.uploadMovie(file, requestVisitPath, request);
        if (movieUrl == null) {
            jsonResult.Error("上传电影媒体文件失败！");
        } else {
            ArrayList<String> resultList = new ArrayList<>();
            resultList.add(movieUrl);
            jsonResult.Success(movieUrl,resultList);
        }
        return jsonResult;
    }
}
